package com.bee.剑指offer;

/**
 * 题目描述
 * 操作给定的二叉树，将其变换为源二叉树的镜像。
 * 比如：    源二叉树
 *             8
 *            /  \
 *           6   10
 *          / \  / \
 *         5  7 9 11
 *         镜像二叉树
 *             8
 *            /  \
 *           10   6
 *          / \  / \
 *         11 9 7  5
 *
 * @version 1.0.0
 * @Author yong.Mr
 * @data 2021-04-17
 */
public class 二叉树的镜像 {

    /**
     * 先序遍历就可以解决
     */
    public static TreeNode Mirror (TreeNode pRoot) {
        if(pRoot == null) {
            return pRoot;
        }
        if(pRoot.left == null && pRoot.right == null) {
            return pRoot;
        }
        TreeNode temp = pRoot.left;
        pRoot.left = pRoot.right;
        pRoot.right = temp;
        Mirror(pRoot.left);
        Mirror(pRoot.right);
        return pRoot;
    }

    public static TreeNode mir (TreeNode pRoot) {
        if(pRoot == null) {
            return pRoot;
        }
        if(pRoot.left == null && pRoot.right == null) {
            return pRoot;
        }
        TreeNode temp = pRoot.left;
        pRoot.left = pRoot.right;
        pRoot.right = temp;
        mir(pRoot.left);
        mir(pRoot.right);
        return pRoot;
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(8);
        root.left = new TreeNode(6);
        root.right = new TreeNode(10);
        root.left.left = new TreeNode(5);
        root.left.right = new TreeNode(7);
        root.right.left = new TreeNode(9);
        root.right.right = new TreeNode(11);
    }
}
